﻿using System;

namespace zijian666.AnyExtensions;

/// <summary>
/// <seealso cref="IComparable{T}"/> 的扩展方法
/// </summary>
public static class IComparableExtensions
{
    #region Obsolete

    /// <summary>
    /// <paramref name="value"/>不能小于 <paramref name="minimum"/>，否则返回<paramref name="minimum"/>
    /// </summary>
    [Obsolete("方法命名有问题，废弃", true)]
    public static T MinOf<T>(this T value, T minimum)
        where T : IComparable<T>
    {
        if (value is null || value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        return value;
    }

    /// <summary>
    /// <paramref name="value"/>不能大于 <paramref name="maximum"/>，否则返回<paramref name="maximum"/>
    /// </summary>
    [Obsolete("方法命名有问题，废弃", true)]
    public static T MaxOf<T>(this T value, T maximum)
        where T : IComparable<T>
    {
        if (value is null || value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value;
    }


    /// <summary>
    /// <paramref name="value"/>不能小于 <paramref name="minimum"/>，否则返回<paramref name="minimum"/>
    /// </summary>
    [Obsolete("方法命名有问题，废弃", true)]
    public static T MinOf<T>(this T? value, T minimum)
        where T : struct, IComparable<T>
    {
        if (value is null || value.Value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        return value.Value;
    }

    /// <summary>
    /// <paramref name="value"/>不能大于 <paramref name="maximum"/>，否则返回<paramref name="maximum"/>
    /// </summary>
    [Obsolete("方法命名有问题，废弃", true)]
    public static T MaxOf<T>(this T? value, T maximum)
        where T : struct, IComparable<T>
    {
        if (value is null || value.Value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value.Value;
    }

    #endregion

    /// <summary>
    /// 如果 <paramref name="value"/> 小于 <paramref name="minimum"/>，则返回<paramref name="minimum"/>，
    /// 如果 大于 <paramref name="maximum"/>，则返回 <paramref name="maximum"/>，
    /// 否则返回<paramref name="value"/>
    /// </summary>
    public static T BetweenOf<T>(this T value, T minimum, T maximum)
        where T : IComparable<T>
    {
        if (value is null || value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        if (value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value;
    }

    /// <summary>
    /// 如果 <paramref name="value"/> 小于 <paramref name="minimum"/>或为 null，则返回<paramref name="minimum"/>，
    /// 如果 大于 <paramref name="maximum"/>，则返回 <paramref name="maximum"/>，
    /// 否则返回<paramref name="value"/>
    /// </summary>
    public static T BetweenOf<T>(this T? value, T minimum, T maximum)
        where T : struct, IComparable<T>
    {
        if (value is null || value.Value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        if (value.Value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value.Value;
    }


    /// <summary>
    /// 如果 <paramref name="value"/> 小于 <paramref name="minimum"/>，返回<paramref name="minimum"/>，否则返回<paramref name="value"/>
    /// </summary>
    public static T GreaterThan<T>(this T value, T minimum)
        where T : IComparable<T>
    {
        if (value is null || value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        return value;
    }

    /// <summary>
    /// 如果 <paramref name="value"/> 大于 <paramref name="maximum"/>，返回<paramref name="maximum"/>，否则返回<paramref name="value"/>
    /// </summary>
    public static T LessThan<T>(this T value, T maximum)
        where T : IComparable<T>
    {
        if (value is null || value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value;
    }


    /// <summary>
    /// 如果 <paramref name="value"/> 小于 <paramref name="minimum"/> 或为 null，返回<paramref name="minimum"/>，否则返回<paramref name="value"/>
    /// </summary>
    public static T GreaterThan<T>(this T? value, T minimum)
        where T : struct, IComparable<T>
    {
        if (value is null || value.Value.CompareTo(minimum) < 0)
        {
            return minimum;
        }
        return value.Value;
    }

    /// <summary>
    /// 如果 <paramref name="value"/> 大于 <paramref name="maximum"/> 或为 null，返回<paramref name="maximum"/>，否则返回<paramref name="value"/>
    /// </summary>
    public static T LessThan<T>(this T? value, T maximum)
        where T : struct, IComparable<T>
    {
        if (value is null || value.Value.CompareTo(maximum) > 0)
        {
            return maximum;
        }
        return value.Value;
    }

}
